package com.hytc.mymall.activity.login;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hytc.mymall.R;
import com.hytc.mymall.base.BaseActivity;
import com.hytc.mymall.braodcastreciver.SmsBroadCastReceiver;
import com.hytc.mymall.http.OkHttpClientManager;
import com.hytc.mymall.http.StringPopCallBack;
import com.hytc.mymall.utils.BaseTools;
import com.hytc.mymall.utils.CryptUtils;
import com.hytc.mymall.utils.MD5Utils;
import com.hytc.mymall.utils.ToastUtils;
import com.hytc.mymall.watcher.BlankWatcher;
import com.hytc.mymall.watcher.RealNameWatcher;

import org.json.JSONObject;

import java.util.HashMap;

public class RegistActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener
{
    private static final int SCANNIN_GREQUEST_CODE = 1;
    private EditText et_mobilenumber;
    private EditText et_password;
    private EditText et_referrer;
    private EditText et_username;
    private EditText et_userpwd;
    private EditText et_verifyCode;
    private Button regist;
    private int registStyle;
    private TextView tv_verifycode;

    public RegistActivity() {
        this.registStyle = 1;
    }

    private void getVerifyCode() {
        final String trim = this.et_mobilenumber.getText().toString().trim();
        final boolean mobileNO = BaseTools.isMobileNO(trim);
        final String trim2 = this.et_referrer.getText().toString().trim();
        if (!mobileNO) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u624b\u673a\u53f7\u7801\uff01", false);
            return;
        }
        if (!TextUtils.isEmpty((CharSequence)trim2)) {
            BaseTools.getRegistVerifyCode((Context)this, this.tv_verifycode, trim, Integer.toString(this.registStyle), this.registStyle, trim2);
            return;
        }
        ToastUtils.showToastWithImage((Context)this, "\u63a8\u8350\u4ebaid\u4e0d\u80fd\u4e3a\u7a7a", false);
    }

    private void initData() {
    }

    private void initView() {
        final RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.rl_back);
        final TextView textView = (TextView)this.findViewById(R.id.toolbar_title);
         //二维码
        final ImageView imageView = (ImageView)this.findViewById(R.id.iv_regist_qrcode);
        this.tv_verifycode = (TextView)this.findViewById(R.id.tv_regist_verifycode);
        this.regist = (Button)this.findViewById(R.id.btn_regist_regist);
        this.et_referrer = (EditText)this.findViewById(R.id.et_regist_id);
        this.et_username = (EditText)this.findViewById(R.id.et_regist_username);
        this.et_mobilenumber = (EditText)this.findViewById(R.id.et_regist_phonenum);
        this.et_verifyCode = (EditText)this.findViewById(R.id.et_regist_verifycode);
        this.et_userpwd = (EditText)this.findViewById(R.id.et_regist_psw);
        this.et_password = (EditText)this.findViewById(R.id.et_regist_ensurepsw);
        final CheckBox checkBox = (CheckBox)this.findViewById(R.id.cb_regist_agreement);
        final TextView textView2 = (TextView)this.findViewById(R.id.tv_regist_agreement);
        relativeLayout.setVisibility(View.VISIBLE);
        textView.setText((CharSequence)this.getResources().getString(R.string.xinshi_regist));
        this.regist.setEnabled(false);
        this.regist.setBackgroundResource(R.drawable.btn_unable_bg);
        relativeLayout.setOnClickListener(this);
        imageView.setOnClickListener(this);
        this.tv_verifycode.setOnClickListener(this);
        this.regist.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(this);
        textView2.setOnClickListener(this);
        this.et_referrer.addTextChangedListener((TextWatcher)new BlankWatcher());
        this.et_username.addTextChangedListener((TextWatcher)new RealNameWatcher());
    }

    private void regist() {
        final String string = Integer.toString(this.registStyle);
        final String string2 = this.et_referrer.getText().toString();
        final String trim = this.et_username.getText().toString().trim();
        final String string3 = this.et_mobilenumber.getText().toString();
        final String string4 = this.et_verifyCode.getText().toString();
        final String string5 = this.et_userpwd.getText().toString();
        final String string6 = this.et_password.getText().toString();
        if (TextUtils.isEmpty((CharSequence)string2.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u63a8\u8350\u4ebaid", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)trim.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u7528\u6237\u540d", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)string3.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u624b\u673a\u53f7", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)string4.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u9a8c\u8bc1\u7801", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)string5.trim()) || TextUtils.isEmpty((CharSequence)string6.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u5bc6\u7801", false);
            return;
        }
        if (!string5.trim().equals(string6.trim())) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u4fdd\u6301\u5bc6\u7801\u4e00\u81f4", false);
            return;
        }
        if (!BaseTools.isPsw(string6)) {
            ToastUtils.showToastWithImage((Context)this, "\u5bc6\u7801\u5fc5\u987b\u4e3a6-16\u4f4d\u7684\u5b57\u6bcd\u6216\u6570\u5b57\u7ec4\u6210", false);
            return;
        }
        final HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("usertype", string);
        hashMap.put("referrer", "0000");
        hashMap.put("usernumber", string2);
        hashMap.put("username", trim);
        hashMap.put("mobilenumber", string3);
        hashMap.put("verifyCode", string4);
        hashMap.put("userpwd", MD5Utils.MD5(string5));
        hashMap.put("password", MD5Utils.MD5(string6));
        OkHttpClientManager.postAsyn("http://www.51ydgy.com/appcommon/appMemberReg", new StringPopCallBack(this, true) {
            @Override
            public void onResponse(final String s) {
                try {
                    final JSONObject jsonObject = new JSONObject(s);
                    final String string = jsonObject.getString("msg");
                    if (jsonObject.getString("code").equals("0")) {
                        ToastUtils.showToastWithImage((Context)RegistActivity.this, string, true);
                        RegistActivity.this.finish();
                        return;
                    }
                    ToastUtils.showToastWithImage((Context)RegistActivity.this, string, false);
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }, hashMap);
    }

    protected void onActivityResult(final int n, final int n2, final Intent intent) {
        super.onActivityResult(n, n2, intent);
        switch (n) {
            case 1: {
                if (n2 != -1) {
                    break;
                }
                final String stringExtra = intent.getStringExtra("result");
                if (!stringExtra.contains("/mobileMemberReg.html#mobileReg/mobileMemberReg") || !stringExtra.contains("&referrerType=")) {
                    ToastUtils.showToastWithImage((Context)this, "\u8bf7\u626b\u63cf\u6b63\u786e\u7684\u63a8\u8350\u7801\uff01", false);
                    return;
                }
                String substring;
                String decryptDES;
                try {
                    substring = stringExtra.substring(12 + stringExtra.indexOf("&usernumber="), stringExtra.length());
                    decryptDES = CryptUtils.decryptDES(stringExtra.substring(14 + stringExtra.indexOf("&referrerType="), stringExtra.indexOf("&usernumber=")));
                    if (this.registStyle == 0) {
                        ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u6ce8\u518c\u7c7b\u578b", false);
                        return;
                    }
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                    return;
                }
                if (decryptDES.equals("2") && this.registStyle != 2) {
                    ToastUtils.showToastWithImage((Context)this, "\u8bf7\u626b\u63cf\u6b63\u786e\u7684\u63a8\u8350\u7801\uff01", false);
                    return;
                }
                try {
                    this.et_referrer.setText((CharSequence)CryptUtils.decryptDES(substring));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
        if (b) {
            this.regist.setEnabled(true);
            this.regist.setBackgroundResource(R.drawable.btn_blue_selector);
            return;
        }
        this.regist.setEnabled(false);
        this.regist.setBackgroundResource(R.drawable.btn_unable_bg);
    }

    public void onClick(final View view) {
        switch (view.getId()) {
            default: {}
            case R.id.rl_back: {
                this.finish();
            }
            case 2131099973: {
//                final Intent intent = new Intent((Context)this, (Class)MipcaActivityCapture.class);
//                intent.setFlags(67108864);
//                intent.putExtra("tag", "RegistActivity");
//                this.startActivityForResult(intent, 1);
            }
            case R.id.tv_regist_verifycode: {
                this.getVerifyCode();
            }
            case 2131099980: {
                this.regist();
            }
            case 2131099979: {
//                final Intent intent2 = new Intent((Context)this, (Class)AgreeMentActivity.class);
//                intent2.putExtra("title", this.getResources().getString(2131362137));
//                this.startActivity(intent2);
            }
        }
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_regist);
        this.initView();
        this.initData();
        final SmsBroadCastReceiver smsBroadCastReceiver = new SmsBroadCastReceiver();
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        this.registerReceiver((BroadcastReceiver)smsBroadCastReceiver, intentFilter);
        smsBroadCastReceiver.setSMSReceiveListener((SmsBroadCastReceiver.SMSReceiveListener)new SmsBroadCastReceiver.SMSReceiveListener() {
            @Override
            public void getVerifyCode(final String text) {
                RegistActivity.this.et_verifyCode.setText((CharSequence)text);
            }
        });
    }

    public void onItemSelected(final AdapterView<?> adapterView, final View view, final int registStyle, final long n) {
        this.registStyle = registStyle;
    }

    public void onNothingSelected(final AdapterView<?> adapterView) {
    }
}

